package md.lanjing.com.module_home.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.lxh.baselibray.base.BaseResponce;
import com.lxh.baselibray.mvp.MvpFragment;
import com.lxh.baselibray.util.ObjectUtils;
import com.lxh.baselibray.util.SizeUtils;
import com.lxh.baselibray.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


import com.lxh.baselibray.constant.Constant;

import com.lxh.baselibray.view.FixRecycleView;
import com.lxh.baselibray.view.water.WaterFlake;
import com.lxh.baselibray.view.water.model.WaterModel;

import md.lanjing.com.module_home.R;
import md.lanjing.com.module_home.R2;
import md.lanjing.com.module_home.bean.CartonResponce;
import md.lanjing.com.module_home.bean.HomeRankResponse;
import md.lanjing.com.module_home.bean.NoticeResponse;
import md.lanjing.com.module_home.bean.UserInfoResponce;
import md.lanjing.com.module_home.contact.HomeContact;
import md.lanjing.com.module_home.ui.activity.MyLevelActivity;
import md.lanjing.com.module_home.ui.activity.TodayProfitActivity;
import md.lanjing.com.module_home.ui.adapter.HomeRankAdapter;
import retrofit2.Response;


/**
 * 首页
 */
public class HomeFragment extends MvpFragment<HomeContact.HomePresent> implements HomeContact.IHomeView {


    @BindView(R2.id.tv_exact_level)
    TextView tvExactLevel;
    @BindView(R2.id.tv_notice)
    TextView tvNotice;
    @BindView(R2.id.water)
    WaterFlake water;
    @BindView(R2.id.rv_rank)
    FixRecycleView rvRank;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;

    private List<WaterModel> mModelList;
    View view=null;

    List<HomeRankResponse.DataBean> mBeanList = new ArrayList<>();
    HomeRankAdapter mAdapter;
    int pageNum = 1;
    private int mLevel;

    @Override
    public void initData(Bundle savedInstanceState) {

        water.setOnWaterItemListener((WaterModel pos,View v) -> {
            view=v;
            mPresent.receiveCarton(context, pos.getProfitId());
        });

        mAdapter = new HomeRankAdapter(R.layout.recy_item_home_rank, mBeanList);
        LinearLayoutManager manager = new LinearLayoutManager(context);
        rvRank.setLayoutManager(manager);
        rvRank.setAdapter(mAdapter);

        mPresent.getUserInfo(context);
        mPresent.getCarton(context);
        mPresent.getNotice(context);
        mPresent.getRank(context, pageNum);
        refresh.setOnRefreshListener(refreshLayout -> {
            pageNum = 1;
            mPresent.getRank(context, pageNum);
        });
        refresh.setOnLoadMoreListener(refreshLayout -> {
            pageNum++;
            mPresent.getRank(context, pageNum);
            mPresent.getUserInfo(context);
            mPresent.getCarton(context);
            mPresent.getNotice(context);
        });
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }


    @OnClick({R2.id.tv_profit, R2.id.tv_my_level})
    public void onViewClicked(View view) {
        int i = view.getId();
        if (i == R.id.tv_profit) {
            startActivity(new Intent(context, TodayProfitActivity.class));

        } else if (i == R.id.tv_my_level) {
            if (mLevel != -1) {
                Intent intent = new Intent(context, MyLevelActivity.class);
                intent.putExtra(MyLevelActivity.LEVEL, mLevel);
                startActivity(intent);
            } else {
                ToastUtils.showLongToast(context, "未激活");
            }

        }
    }

    @Override
    public void getNoticeData(Response<NoticeResponse> response) {
        refresh.finishRefresh();
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            NoticeResponse.DataBean data = response.body().getData();
            if (ObjectUtils.isEmpty(data)) return;
            tvNotice.setText(data.getContent());
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    public void getRankData(Response<HomeRankResponse> response) {
        refresh.finishRefresh();
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            List<HomeRankResponse.DataBean> data = response.body().getData();
            if (pageNum == 1) {
                mBeanList.clear();
            }
            mBeanList.addAll(data);
            mAdapter.notifyDataSetChanged();

            if (data.size() == 10) {
                refresh.setEnableLoadMore(true);
            } else {
                refresh.setEnableLoadMore(false);
            }
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    public void getUserInfoData(Response<UserInfoResponce> response) {
        refresh.finishRefresh();
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            UserInfoResponce.DataBean data = response.body().getData();
            if (ObjectUtils.isEmpty(data)) return;
            int level = data.getLevel();
            mLevel=level;
            String levelText = null;
            switch (level) {
                case -1:
                    levelText="未激活";
                    break;
                case 0:
                    levelText = "一般投资人";
                    break;
                case 1:
                    levelText = "节能经纪人";
                    break;
                case 2:
                    levelText = "绿能源经纪人";
                    break;
                case 3:
                    levelText = "赋能源经纪人";
                    break;
                case 4:
                    levelText = "超能源经纪人";
                    break;
                case 5:
                    levelText = "运营中心";
                    break;
            }
            tvExactLevel.setText(levelText);
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    public void getCartonData(Response<CartonResponce> response) {
        refresh.finishRefresh();
        if (response.body().getCode() == Constant.SUCCESS_CODE) {

            List<CartonResponce.DataBean> data = response.body().getData();
            if (ObjectUtils.isEmpty(data)) return;
            mModelList = new ArrayList<>();
            for (int i = 0; i < data.size(); i++) {
                mModelList.add(new WaterModel(data.get(i).getAbsorbCawd() + "", data.get(i).getId()));
            }
            water.setModelList(mModelList, SizeUtils.dp2px(context, 180), SizeUtils.dp2px(context, 200));
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    public void getReceiveCartonData(Response<BaseResponce> response) {
        refresh.finishRefresh();
        if (response.body().getCode() == Constant.SUCCESS_CODE) {
            water.collectAnimator(view);
        } else {
            ToastUtils.showLongToast(context, response.body().getMsg());
        }
    }

    @Override
    protected HomeContact.HomePresent createPresent() {
        return new HomeContact.HomePresent();
    }
}
